package com.teach.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.bean.PageVo;
import com.common.bean.QueryCondition;
import com.common.utils.SQLFilter;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author: Hzz
 * @Date: Created in 20:21 2020/4/27
 */
public class PageUtil <T>{

    public static final PageVo selectPage(ISelect select,QueryCondition params){
        PageInfo<Object> pageInfo = PageHelper.startPage(params.getPage(), params.getPageSize(),params.getOrder()).doSelectPageInfo(select);
        PageVo pageVo=new PageVo(pageInfo.getList(),pageInfo.getTotal(),pageInfo.getPageSize(),pageInfo.getPages());
        return pageVo;
    }

    public IPage<T> getPage(QueryCondition params) {
        return this.getPage(params, null, false);
    }
    public IPage<T> getPage(QueryCondition params, String defaultOrderField, boolean isAsc) {
        //分页参数
        long curPage = params.getPage();
        long pagesize = params.getPageSize();
        //分页对象
        Page<T> page = new Page<>(curPage, pagesize);

        //分页参数
//        params.put(Constant.PAGE, page);

        //排序字段
        String orderField = SQLFilter.sqlInject(params.getSidx());
        String order = params.getOrder();

        //前端字段排序
        if(StringUtils.isNotEmpty(orderField) && StringUtils.isNotEmpty(order)){
            if("asc".equalsIgnoreCase(order)) {
                return page.setAsc(orderField);
            }else {
                return page.setDesc(orderField);
            }
        }

        //默认排序
        if(isAsc) {
            page.setAsc(defaultOrderField);
        }else {
            page.setDesc(defaultOrderField);
        }

        return page;
    }

}
